package site

import (
	"context"
	"errors"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/repository/base/site"
	basePb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/base"
	"time"
)

type siteService struct {
	siteRepo site.SiteRepositoryIface
}

func NewSiteService(siteRepo site.SiteRepositoryIface) SiteServiceIface {
	return &siteService{
		siteRepo: siteRepo,
	}
}

func (s *siteService) SaveSiteInfo(ctx context.Context, request *basePb.SaveSiteInfoRequest) error {
	// 检查站点配置信息
	siteInfo, err := s.GetSiteInfo(ctx)
	if err != nil {
		return err
	}

	siteInfo.Logo = request.GetSiteInfo().GetLogo()
	siteInfo.ModifiedOn = time.Now()

	// 保存站点配置信息
	if err := s.siteRepo.SaveSiteInfo(ctx, siteInfo); err != nil {
		return err
	}

	return nil
}

func (s *siteService) GetSiteInfo(ctx context.Context) (*site.SiteInfoPo, error) {
	// 检查站点配置信息
	siteInfo, err := s.siteRepo.GetSiteInfo(ctx)
	if err != nil {
		return nil, err
	} else if siteInfo.Id == 0 {
		return nil, errors.New("读取站点信息失败")
	}

	return siteInfo, nil
}
